package com.serviceprovider.business.module.admin.faq.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.faq.model.Answer;
import com.serviceprovider.business.module.admin.faq.model.Category;
import com.serviceprovider.business.module.admin.faq.model.Faq;
import com.serviceprovider.business.module.admin.faq.model.Question;
import com.serviceprovider.business.module.admin.faq.services.dao.interfaces.IFaqService;
import com.serviceprovider.business.module.admin.user.services.dao.interfaces.IUserService;
import com.serviceprovider.business.module.common.Page;
import com.serviceprovider.business.module.user.model.impl.User;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminFaqController {
    @Autowired
    ApplicationConfigProperties applicationConfigProperties;
    @Autowired
    private IFaqService         faqService;
    @Autowired
    IUserService                userService;

    @RequestMapping(
        value  = RequestConstants.FAQRequests.CATEGORY,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaqCategory(HttpServletRequest request, HttpSession sess) {
        ModelAndView mav        = new ModelAndView(RequestConstants.FAQRequests.VIEW_CATEGORY);
        int          recordSize = applicationConfigProperties.getRecordSize("faq_category.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "category_id"
                            : request.getParameter("sortColumn");
        Category newCat = new Category();

        mav.addObject("newCategory", newCat);
        mav.addObject("listCategories", faqService.listCategories(pageNo, recordSize, sortColumn, sortOrder));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.CATEGORY,
        method = RequestMethod.POST
    )
    public ModelAndView createNewCategory(@ModelAttribute("newCategory") Category form, BindingResult result) {
        faqService.createCategory(form);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.CATEGORY);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.UPDATE_CATEGORY,
        method = RequestMethod.GET
    )
    public ModelAndView updateCategory(HttpServletRequest request) {
        int      id   = Integer.parseInt(request.getParameter("id"));
        String   name = request.getParameter("name");
        Category cat  = new Category();

        cat.setId(id);
        cat.setName(name);
        faqService.modifyCategory(cat);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.CATEGORY);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.DELETE_CATEGORY,
        method = RequestMethod.GET
    )
    public ModelAndView deleteCategory(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));

        faqService.deleteCategory(id);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.CATEGORY);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.FAQ_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaqQuestion(HttpSession sess, HttpServletRequest request) {
        ModelAndView mav    = new ModelAndView(RequestConstants.FAQRequests.VIEW_FAQ_QUESTION);
        int          viewId = 0;

        try {
            viewId = Integer.parseInt(request.getParameter("view"));
        } catch (NumberFormatException ex) {}

        int categoryId = 0;

        try {
            categoryId = Integer.parseInt(request.getParameter("catId"));
        } catch (NumberFormatException ex) {}

        String         refNumber     = (request.getParameter("refNumber") == null)
                                       ? ""
                                       : request.getParameter("refNumber");
        Page<Question> listQuestions = null;
        int            recordSize    = applicationConfigProperties.getRecordSize("faq_question.records.size");
        int            pageNo        = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "question_id"
                            : request.getParameter("sortColumn");

        if (viewId == 1) {
            listQuestions = faqService.listRepliedQuestions(categoryId, refNumber, pageNo, recordSize, sortColumn,
                    sortOrder);
        } else if (viewId == 2) {
            listQuestions = faqService.listUnansweredQuestions(categoryId, refNumber, pageNo, recordSize, sortColumn,
                    sortOrder);
        } else {
            listQuestions = faqService.listQuestions(categoryId, refNumber, pageNo, recordSize, sortColumn, sortOrder);
        }

        mav.addObject("listQuestions", listQuestions);

        List<Category> listCategories = faqService.listCategories();

        mav.addObject("listCategories", listCategories);
        mav.addObject("categoryId", categoryId);
        mav.addObject("refNumber", refNumber);
        mav.addObject("viewId", viewId);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.REPLY_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaqReplyQuestion(HttpServletRequest request) {
        ModelAndView mav      = new ModelAndView(RequestConstants.FAQRequests.VIEW_REPLY_QUESTION);
        int          id       = Integer.parseInt(request.getParameter("id"));
        Question     question = faqService.getQuestion(id);

        mav.addObject("question", question);

        Answer newAnswer = new Answer();

        mav.addObject("newAnswer", newAnswer);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.REPLY_QUESTION,
        method = RequestMethod.POST
    )
    public ModelAndView createNewAnswer(@ModelAttribute("newAnswer") Answer form, BindingResult result,
            HttpServletRequest request) {
        int questionId = Integer.parseInt(request.getParameter("questionId"));
        int topRank    = Integer.parseInt(request.getParameter("topRank"));

        form.setQuestionId(questionId);
        form.setCreated(new Date());
        form.setUpdated(new Date());
        faqService.createAnswer(form);
        faqService.rankQuestion(questionId, topRank);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ_QUESTION);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.VIEW_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView adminViewQuestion(HttpServletRequest request) {
        int          id       = Integer.parseInt(request.getParameter("id"));
        Question     question = faqService.getQuestion(id);
        Answer       answer   = faqService.getAnswerForQuestion(id);
        ModelAndView mav      = new ModelAndView(RequestConstants.FAQRequests.VIEW_VIEW_QUESTION);

        mav.addObject("question", question);
        mav.addObject("answer", answer);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.VIEW_SP_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView spViewQuestion(HttpServletRequest request) {
        int          id       = Integer.parseInt(request.getParameter("id"));
        Question     question = faqService.getQuestion(id);
        Answer       answer   = faqService.getAnswerForQuestion(id);
        ModelAndView mav      = new ModelAndView(RequestConstants.FAQRequests.VIEW_VIEW_SP_QUESTION);

        mav.addObject("question", question);
        mav.addObject("answer", answer);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.DELETE_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaqDeleteQuestion(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));

        faqService.deleteQuestion(id);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ_QUESTION);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.CREATE_QUESTION_FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView adminQuestionCreateFaq(HttpServletRequest request) {
        int      id       = Integer.parseInt(request.getParameter("id"));
        Faq      faq      = new Faq();
        Question question = faqService.getQuestion(id);
        Answer   answer   = faqService.getAnswerForQuestion(id);

        faq.setQuestion(question.getQuestion());
        faq.setTag(answer.getTag());
        faq.setAnswer(answer.getAnswer());
        faq.setCreated(new Date());
        faq.setUpdated(new Date());
        faqService.modifyQuestionFaq(question.getId(), 1);
        faqService.createFaq(faq);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ_QUESTION);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.FAQ_ANSWER,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaqAnswer(HttpSession sess) {
        ModelAndView mav         = new ModelAndView(RequestConstants.FAQRequests.VIEW_FAQ_ANSWER);
        List<Answer> listAnswers = faqService.listAnswers();

        mav.addObject("listAnswers", listAnswers);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.UPDATE_ANSWER,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaqEnterUpdateAnswer(HttpServletRequest request) {
        ModelAndView mav      = new ModelAndView(RequestConstants.FAQRequests.VIEW_UPDATE_ANSWER);
        int          id       = Integer.parseInt(request.getParameter("id"));
        Question     question = faqService.getQuestion(id);
        Answer       answer   = faqService.getAnswerForQuestion(id);

        mav.addObject("answerUpdate", answer);
        mav.addObject("question", question);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.UPDATE_ANSWER,
        method = RequestMethod.POST
    )
    public ModelAndView updateAnswer(@ModelAttribute("answerUpdate") Answer form, BindingResult result,
                                     HttpServletRequest request) {
        int answerId   = Integer.parseInt(request.getParameter("answerId"));
        int questionId = Integer.parseInt(request.getParameter("questionId"));
        int topRank    = Integer.parseInt(request.getParameter("topRank"));

        form.setId(answerId);
        form.setUpdated(new Date());
        faqService.modifyAnswer(form);
        faqService.rankQuestion(questionId, topRank);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ_QUESTION);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.DELETE_ANSWER,
        method = RequestMethod.GET
    )
    public ModelAndView deleteAnswer(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));

        faqService.deleteAnswer(id);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ_ANSWER);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView adminFaq(HttpSession sess, HttpServletRequest request) {
        ModelAndView mav        = new ModelAndView(RequestConstants.FAQRequests.VIEW_FAQ);
        int          recordSize = applicationConfigProperties.getRecordSize("faq_answer.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "faq_id"
                            : request.getParameter("sortColumn");
        Page<Faq> listFaqs = faqService.listFaqs(pageNo, recordSize, sortColumn, sortOrder);

        mav.addObject("listFaqs", listFaqs);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.WRITE_FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView adminWriteFaq(HttpSession sess) {
        ModelAndView mav    = new ModelAndView(RequestConstants.FAQRequests.VIEW_WRITE_FAQ);
        Faq          newFaq = new Faq();

        mav.addObject("newFaq", newFaq);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.FAQ_VIEW,
        method = RequestMethod.GET
    )
    public ModelAndView adminViewFaq(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView(RequestConstants.FAQRequests.VIEW_FAQ_VIEW);
        int          id  = Integer.parseInt(request.getParameter("id"));
        Faq          faq = faqService.getFaq(id);

        mav.addObject("faq", faq);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.WRITE_FAQ,
        method = RequestMethod.POST
    )
    public ModelAndView adminCreateFaq(@ModelAttribute("newFaq") Faq form, BindingResult result) {
        form.setRating(0);
        form.setCreated(new Date());
        form.setUpdated(new Date());
        faqService.createFaq(form);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.UPDATE_FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView adminEnterUpdateFaq(HttpServletRequest request) {
        int          id  = Integer.parseInt(request.getParameter("id"));
        Faq          faq = faqService.getFaq(id);
        ModelAndView mav = new ModelAndView(RequestConstants.FAQRequests.VIEW_UPDATE_FAQ);

        mav.addObject("faq", faq);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.UPDATE_FAQ,
        method = RequestMethod.POST
    )
    public ModelAndView adminUpdateFaq(@ModelAttribute("faq") Faq form, BindingResult result) {
        form.setUpdated(new Date());
        faqService.modifyFaq(form);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.DELETE_FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView adminDeleteFaq(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));

        faqService.deleteFaq(id);

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.FAQRequests.FAQ);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_FAQ_HOME,
        method = RequestMethod.GET
    )
    public ModelAndView spFaqHome(HttpSession sess) {
        ModelAndView mav            = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_FAQ_HOME);
        List<Answer> listTopAnswers = faqService.listTopTenAnswers();

        mav.addObject("listAnswers", listTopAnswers);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_FAQ_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView spFaqQuestion(HttpSession sess) {
        ModelAndView mav         = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_FAQ_QUESTION);
        Question     newQuestion = new Question();
        User         user        = userService.getByEmail(sess.getAttribute("loginUserEmail").toString());

        newQuestion.setFirstName(user.getFirstName());
        newQuestion.setLastName(user.getLastName());
        newQuestion.setEmail(user.getEmail());
        newQuestion.setUserId(user.getId());

        List<Category> listCategories = faqService.listCategories();

        mav.addObject("newQuestion", newQuestion);
        mav.addObject("listCategories", listCategories);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_FAQ_QUESTION,
        method = RequestMethod.POST
    )
    public ModelAndView spCreateNewQuestion(@ModelAttribute("newQuestion") Question form, BindingResult result) {
        form.setCreated(new Date());
        form.setUpdated(new Date());
        form.setRefNumber(form.getUserId() + "-" + form.getOrderNumber() + "-"
                          + userService.getUserConnected().getEfin());
        faqService.createQuestion(form);

        ModelAndView mav = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_FAQ_DONE);

        mav.addObject("refNumber", form.getRefNumber());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_FAQ_ANSWER,
        method = RequestMethod.GET
    )
    public ModelAndView spFaqAnswer(HttpServletRequest request) {
        ModelAndView mav        = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_FAQ_ANSWER);
        String       keyword    = request.getParameter("keyword");
        int          recordSize = applicationConfigProperties.getRecordSize("faq_answer.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "answer_id"
                            : request.getParameter("sortColumn");
        Page<Answer> listAnswers = faqService.findAnswers(keyword, pageNo, recordSize, sortColumn, sortOrder);

        mav.addObject("listAnswers", listAnswers);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_VIEW_ANSWER,
        method = RequestMethod.GET
    )
    public ModelAndView spViewAnswer(HttpServletRequest request) {
        ModelAndView mav    = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_VIEW_ANSWER);
        int          id     = Integer.parseInt(request.getParameter("id"));
        Answer       answer = faqService.getAnswer(id);

        mav.addObject("answer", answer);

        List<Answer> listUserfulAnswers = faqService.listUsefulAnswers(id);

        mav.addObject("usefulAnswers", listUserfulAnswers);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_LIST_FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView spListFaq(HttpServletRequest request) {
        ModelAndView mav        = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_LIST_FAQ);
        int          recordSize = applicationConfigProperties.getRecordSize("list_faq.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "faq_id"
                            : request.getParameter("sortColumn");
        Page<Faq> listFaqs = faqService.listFaqs(pageNo, recordSize, sortColumn, sortOrder);

        mav.addObject("listFaqs", listFaqs);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_VIEW_FAQ,
        method = RequestMethod.GET
    )
    public ModelAndView spViewFaq(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_VIEW_FAQ);
        int          id  = Integer.parseInt(request.getParameter("id"));
        Faq          faq = faqService.getFaq(id);

        mav.addObject("faq", faq);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_MY_QUESTION,
        method = RequestMethod.GET
    )
    public ModelAndView spViewMyQuestion(HttpSession sess, HttpServletRequest request) {
        ModelAndView   mav           = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_MY_QUESTION);
        Page<Question> listQuestions = null;
        int            recordSize    = applicationConfigProperties.getRecordSize("my_question.records.size");
        int            pageNo        = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "question_id"
                            : request.getParameter("sortColumn");
        User user = userService.getByEmail(sess.getAttribute("loginUserEmail").toString());

        listQuestions = faqService.listQuestionsByUser(user.getId(), pageNo, recordSize, sortColumn, sortOrder);
        mav.addObject("listQuestions", listQuestions);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.FAQRequests.SP_RATE_ANSWER,
        method = RequestMethod.GET
    )
    public ModelAndView spRateAnswer(HttpSession sess, HttpServletRequest request) {
        ModelAndView mav  = new ModelAndView(RequestConstants.FAQRequests.VIEW_SP_VIEW_ANSWER);
        int          id   = Integer.parseInt(request.getParameter("id"));
        int          rate = Integer.parseInt(request.getParameter("rate"));

        faqService.rateAnswer(id, rate);

        Answer answer = faqService.getAnswer(id);

        mav.addObject("answer", answer);

        List<Answer> listUserfulAnswers = faqService.listUsefulAnswers(id);

        mav.addObject("usefulAnswers", listUserfulAnswers);

        return mav;
    }
}



